package com.kaizeli.website.controller;

import com.kaizeli.website.pojo.dto.base.PageDTO;
import com.kaizeli.website.pojo.entity.CustomerDO;
import com.kaizeli.website.pojo.vo.CustomerVO;
import com.kaizeli.website.query.CustomerQuery;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.CustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/customers")
@RequiredArgsConstructor
@Tag(name = "客户管理", description = "客户信息管理相关接口")
@SecurityRequirement(name = "Bearer Authentication")
public class CustomerController {
    
    private final CustomerService customerService;
    
    @Operation(summary = "分页查询客户列表", description = "支持按客户名称、编码、状态等条件分页查询客户信息")
    @GetMapping("/page")
    public Result<PageDTO<CustomerVO>> pageQuery(CustomerQuery query) {
        log.info("分页查询客户列表，查询条件：{}", query);
        PageDTO<CustomerVO> result = customerService.pageQuery(query);
        return Result.success(result);
    }
    
    @Operation(summary = "根据ID查询客户详情", description = "根据客户ID查询客户详细信息")
    @GetMapping("/{id}")
    public Result<CustomerVO> getById(
            @Parameter(description = "客户ID", required = true, example = "1")
            @PathVariable Integer id) {
        log.info("根据ID查询客户详情，ID：{}", id);
        CustomerVO result = customerService.getById(id);
        return Result.success(result);
    }
    
    @Operation(summary = "根据编码查询客户", description = "根据客户唯一编码查询客户信息")
    @GetMapping("/code/{code}")
    public Result<CustomerVO> getByCode(
            @Parameter(description = "客户编码", required = true, example = "CUST001")
            @PathVariable String code) {
        log.info("根据编码查询客户，编码：{}", code);
        CustomerVO result = customerService.getByCode(code);
        return Result.success(result);
    }
    
    @Operation(summary = "新增客户", description = "创建新的客户信息")
    @PostMapping
    public Result<Integer> save(
            @Parameter(description = "客户信息", required = true)
            @RequestBody CustomerDO customerDO) {
        log.info("新增客户，客户信息：{}", customerDO);
        // 清除ID，确保使用数据库自增ID
        customerDO.setId(null);
        Integer result = customerService.save(customerDO);
        return Result.success(result);
    }
    
    @Operation(summary = "更新客户信息", description = "根据ID更新客户信息")
    @PutMapping("/{id}")
    public Result<Boolean> update(
            @Parameter(description = "客户ID", required = true, example = "1")
            @PathVariable Integer id,
            @Parameter(description = "更新的客户信息", required = true)
            @RequestBody CustomerDO customerDO) {
        log.info("更新客户信息，ID：{}，更新信息：{}", id, customerDO);
        customerDO.setId(id);
        Boolean result = customerService.update(customerDO);
        return Result.success(result);
    }
    
    @Operation(summary = "根据ID删除客户", description = "根据客户ID删除客户信息")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(
            @Parameter(description = "客户ID", required = true, example = "1")
            @PathVariable Integer id) {
        log.info("根据ID删除客户，ID：{}", id);
        Boolean result = customerService.deleteById(id);
        return Result.success(result);
    }
    
    @Operation(summary = "根据状态查询客户列表", description = "根据客户状态查询客户列表")
    @GetMapping("/status/{status}")
    public Result<List<CustomerVO>> listByStatus(
            @Parameter(description = "客户状态", required = true, example = "active")
            @PathVariable String status) {
        log.info("根据状态查询客户列表，状态：{}", status);
        List<CustomerVO> result = customerService.listByStatus(status);
        return Result.success(result);
    }
    
    @Operation(summary = "查询所有客户列表", description = "查询系统中所有客户信息")
    @GetMapping("/all")
    public Result<List<CustomerVO>> listAll() {
        log.info("查询所有客户列表");
        List<CustomerVO> result = customerService.listAll();
        return Result.success(result);
    }
    
    @Operation(summary = "搜索客户", description = "根据名称或编码搜索客户信息")
    @GetMapping("/search")
    public Result<List<CustomerVO>> search(
            @Parameter(description = "搜索关键词（客户名称或编码）", required = true, example = "阿里巴巴")
            @RequestParam String keyword) {
        log.info("搜索客户，关键词：{}", keyword);
        List<CustomerVO> result = customerService.search(keyword);
        return Result.success(result);
    }
}

